<template>
  <el-form :model="formModel" ref="formRef" :show-message="false" size="small">
    <ElCard>
      <CardBox header="个人信息" v-if="!isHealthCheck">
        <ElFormItem label="姓名">
          <ElInput v-model="formModel.a00_03_05" />
        </ElFormItem>
        <ElFormItem label="健康档案号">
          <ElInput v-model="formModel.a00_03_03" />
        </ElFormItem>
        <ElFormItem label="体检日期">
          <ElDatePicker
            v-model="formModel.a00_04_03"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </ElFormItem>
        <ElFormItem label="责任医生">
          <ElInput v-model="formModel.a00_04_04" />
        </ElFormItem>
        <ElFormItem label="身份证号">
          <ElInput v-model="formModel.id_no" />
        </ElFormItem>
      </CardBox>
      <br />
      <CardBox header="症状">
        <ElFormItem label=" ">
          <ElCheckboxGroup
            v-model="formModel.a00_04_05"
            @change="handleArrayExcludeClear($event, '25', ['a00_04_06'])"
          >
            <ElCheckbox
              v-for="(value, key) in formSource.a00_04_05"
              :label="key"
              :key="key"
              >{{ value }}</ElCheckbox
            >
          </ElCheckboxGroup>
        </ElFormItem>
        <ElFormItem
          label="其他"
          prop="a00_04_06"
          :required="formModel.a00_04_05?.includes('25')"
        >
          <ElInput
            v-model="formModel.a00_04_06"
            :disabled="!formModel.a00_04_05?.includes('25')"
          />
        </ElFormItem>
      </CardBox>
      <br />
      <CardBox header="一般情况">
        <ElFormItem label="体温">
          <ElInput type="number" v-model="formModel.a00_04_07" style="width: 130px">
<!--            <template #append>℃</template>-->
          </ElInput>
        </ElFormItem>
        <ElFormItem label="脉率">
          <ElInput type="number" v-model="formModel.a00_04_08" style="width: 130px">
<!--            <template #append>次/分钟</template>-->
          </ElInput>
        </ElFormItem>
        <ElFormItem label="呼吸频率">
          <ElInput type="number" v-model="formModel.a00_04_09" style="width: 130px">
<!--            <template #append>次/分钟</template>-->
          </ElInput>
        </ElFormItem>
        <br />
        <ElFormItem label="左侧血压(高)" v-if="!isHealthCheck">
          <ElInput
            type="number"
            v-model="formModel.a00_04_10"
            class="input-number-width"
          >
            <template #append>mmHg</template>
          </ElInput>
        </ElFormItem>
        <ElFormItem label="左侧血压(低)" v-if="!isHealthCheck">
          <ElInput type="number" v-model="formModel.a00_04_11">
            <template #append>mmHg</template>
          </ElInput>
        </ElFormItem>
        <ElFormItem label="血压右侧(高)" v-if="!isHealthCheck">
          <ElInput type="number" v-model="formModel.a00_04_12">
            <template #append>mmHg</template>
          </ElInput>
        </ElFormItem>
        <ElFormItem label="血压右侧(低)" v-if="!isHealthCheck">
          <ElInput type="number" v-model="formModel.a00_04_13">
            <template #append>mmHg</template>
          </ElInput>
        </ElFormItem>
        <ElFormItem label="身高" v-if="!isHealthCheck">
          <ElInput
            type="number"
            v-model="formModel.a00_04_14"
            @change="computedHW"
          >
            <template #append>cm</template>
          </ElInput>
        </ElFormItem>
        <ElFormItem label="体重" v-if="!isHealthCheck">
          <ElInput
            type="number"
            v-model="formModel.a00_04_15"
            @change="computedHW"
          >
            <template #append>kg</template>
          </ElInput>
        </ElFormItem>
        <ElFormItem label="腰围" v-if="!isHealthCheck">
          <ElInput type="number" v-model="formModel.a00_04_16">
            <template #append>cm</template>
          </ElInput>
        </ElFormItem>
        <ElFormItem label="体质指数" v-if="!isHealthCheck">
          <ElInput type="number" v-model="formModel.a00_04_17">
            <template #append>cm</template>
          </ElInput>
        </ElFormItem>
      </CardBox>
      <br />
      <CardBox header="老年人">
        <ElFormItem label="老年人健康自我评估">
          <ElRadioGroup v-model="formModel.a00_04_184">
            <ElRadio
              v-for="(value, key) in formSource.a00_04_184"
              :key="key"
              :label="key"
              >{{ value }}</ElRadio
            >
          </ElRadioGroup>
        </ElFormItem>
        <ElFormItem label="老年人自理能力评估">
          <ElRadioGroup v-model="formModel.a00_04_205">
            <ElRadio
              v-for="(value, key) in formSource.a00_04_205"
              :key="key"
              :label="key"
              >{{ value }}</ElRadio
            >
          </ElRadioGroup>
          <!--          <ElButton @click="elderlyForm">老年人评估表</ElButton>-->
        </ElFormItem>
        <ElFormItem label="老年人认知功能">
          <ElRadioGroup v-model="formModel.a00_04_20">
            <ElRadio
              v-for="(value, key) in formSource.a00_04_20"
              :key="key"
              :label="key"
              >{{ value }}</ElRadio
            >
          </ElRadioGroup>
        </ElFormItem>
        <ElFormItem
          label="简易状态检查，总分"
          prop="a00_04_21"
          :required="formModel.a00_04_20 === '2'"
        >
          <ElInput
            type="number"
            v-model="formModel.a00_04_21"
            :readonly="formModel.a00_04_20 !== '2'"
          />
        </ElFormItem>
        <ElFormItem label="老年人感情状态">
          <ElRadioGroup v-model="formModel.a00_04_22">
            <ElRadio
              v-for="(value, key) in formSource.a00_04_20"
              :key="key"
              :label="key"
              >{{ value }}</ElRadio
            >
          </ElRadioGroup>
        </ElFormItem>
        <ElFormItem
          label="老年人抑郁评分检查，总分"
          prop="a00_04_23"
          :required="formModel.a00_04_22 === '2'"
        >
          <ElInput
            type="number"
            v-model="formModel.a00_04_23"
            :readonly="formModel.a00_04_22 !== '2'"
          />
        </ElFormItem>
      </CardBox>
    </ElCard>
  </el-form>
</template>

<script lang="ts" setup>
import { IA00_03, IBaseInfo } from "@/model";
import { reactive, ref, toRaw, watch } from "vue";
import { tjwx } from "@/dict";
import { ElForm } from "element-plus";
import formUtils from "./formUtils";
import { cloneDeep } from "lodash-es";
const isHealthCheck = ref<boolean>(import.meta.env.MODE === "health_check");

const props = defineProps<{
  baseInfo: Partial<IBaseInfo>;
  personalArchive?: Partial<IA00_03>;
  userName?: string;
}>();

const formSource = reactive(tjwx);

const formModel = ref(cloneDeep(props.baseInfo));

// watch(
//   () => {
//     return props.baseInfo;
//   },
//   (val) => {
//     formModel.value = val;
//   }
// );

// 兼容终端
watch(
  () => {
    return props.userName;
  },
  (val) => {
    if (val) {
      formModel.value.a00_03_05 = val;
    }
  },
  {
    immediate: true,
  }
);
const personalArchive = ref(cloneDeep(props.personalArchive));

watch(
  () => {
    return props.personalArchive;
  },
  (val) => {
    personalArchive.value = val;
  }
);
// console.log(formModel)
const formRef = ref<InstanceType<typeof ElForm>>();
const validateForm = () => {
  return formRef.value?.validate();
};
const getData = () => {
  return toRaw(formModel.value);
};
// 计算体质指数
const computedHW = () => {
  formModel.value.a00_04_17 = undefined;
  if (formModel.value?.a00_04_15 && formModel.value?.a00_04_14) {
    formModel.value.a00_04_17 = Number(
      (
        formModel?.value?.a00_04_15 /
        Math.pow(formModel.value?.a00_04_14 / 100, 2)
      ).toFixed(2)
    );
  }
};

const { handleArrayExcludeClear } = formUtils(formModel);
defineExpose({ validateForm, getData });
</script>
<style scoped lang="scss"></style>
